<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4_展开运算符</title>
</head>
<body>
  <script>
    let arr1 = [1,3,5,7,9]
    let arr2 = [2,4,6,8,10]
    console.log(...arr1)          // 展开一个数组

    let arr3 = [...arr1, ...arr2] // 连接数组
    console.log(arr3)

    function sum01(...numbers) {
      console.log(numbers)
      return numbers.reduce((preValue, currValue) => {
        return preValue + currValue
      })
    }

    let sumValue01 = sum01(1,2,3,4,5)
    console.log(sumValue01)

    function sum02(...numbers) {
      // ...扩展运算符会把参数转化为数组
      console.log(numbers)
      return numbers[0].reduce((preValue, currValue) => {
        return preValue + currValue
      })
    }

    let sumValue02 = sum02(arr3)
    console.log(sumValue02)

    let person = {name: "Tom", age: 18}
    let person2 = person
    person.name = "Jerry"
    console.log(person2.name)

    // 构造字面量对象时使用展开语法
    person2 = {...person}
    // console.log(...person)  // 报错，展开运算符不能展开对象
    person.name = "Mili"
    console.log(person2.name)

    // 合并
    let person3 = {...person, name: 'Jack'}
    console.log(person3)

  </script>
</body>
</html>